package com.mmbang.util;


import org.bson.types.ObjectId;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoOptions;
import com.mongodb.ServerAddress;

public class DbTools {
	
	/*
	static Mongo m1 = null;
	static Mongo m2 = null;
	static{
		try {
			List<ServerAddress> list1= new ArrayList<ServerAddress>();
			List<ServerAddress> list2= new ArrayList<ServerAddress>();
			//#YMPT-app-apps-babyname-base-cache-circles-city-data-iguangjie-messages-products-survey-tools-users-yuer

		
			

//			ServerAddress sap0 = new ServerAddress("192.168.1.51",27017);
//			ServerAddress sas1 = new ServerAddress("192.168.1.51",27017);
//			ServerAddress sas2 = new ServerAddress("192.168.1.51",27017);

//			bangs   43.9326171875GB
//			checkins        1.953125GB
//			crawler			
			
			
			ServerAddress sap0 = new ServerAddress("192.168.0.26",26017);
			ServerAddress sas1 = new ServerAddress("192.168.0.16",26017);
			ServerAddress sas2 = new ServerAddress("192.168.0.15",26017);			
			
			
			
//			ServerAddress sap0 = new ServerAddress("192.168.0.16",25017);
//			ServerAddress sas1 = new ServerAddress("192.168.0.16",25017);
//			ServerAddress sas2 = new ServerAddress("192.168.0.16",25017);				
			
//			ServerAddress sap0 = new ServerAddress("192.168.100.23",25017);
//			ServerAddress sas1 = new ServerAddress("192.168.100.23",25017);
//			ServerAddress sas2 = new ServerAddress("192.168.100.23",25017);			
			
			
//			ServerAddress sap0 = new ServerAddress("192.168.100.23",25017);
//			ServerAddress sas1 = new ServerAddress("192.168.100.23",25017);
//			ServerAddress sas2 = new ServerAddress("192.168.100.23",25017);			

//			ServerAddress sap3 = new ServerAddress("192.168.1.51",27017);
//			ServerAddress sas4 = new ServerAddress("192.168.1.51",27017);
//			ServerAddress sas5 = new ServerAddress("192.168.1.51",27017);			

//			其他
//			city
//			circles
//			users库			
			
			ServerAddress sap3 = new ServerAddress("192.168.0.24",25017);
			ServerAddress sas4 = new ServerAddress("192.168.0.25",25017);
			ServerAddress sas5 = new ServerAddress("192.168.0.13",25017);			
			
			
			
//			ServerAddress sap3 = new ServerAddress("192.168.0.24",25017);
//			ServerAddress sas4 = new ServerAddress("192.168.0.25",25017);
//			ServerAddress sas5 = new ServerAddress("192.168.0.13",25017);
			
			
//			ServerAddress sap3 = new ServerAddress("192.168.100.23",25017);
//			ServerAddress sas4 = new ServerAddress("192.168.100.23",25017);
//			ServerAddress sas5 = new ServerAddress("192.168.100.23",25017);			
			
			list1.add(sap0);
			list1.add(sas1);
			list1.add(sas2);
			MongoOptions mongoOptions=new MongoOptions();
			m1=new Mongo(list1,mongoOptions);
			
			list2.add(sap3);
			list2.add(sas4);
			list2.add(sas5);			
			m2=new Mongo(list2,mongoOptions);
			
			
			//m = new Mongo(list);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			System.out.println("数据库联接异常"+e.getMessage());
		}
	}
	
	public Mongo getMongo1() {
		return m1;
	}
	
	public Mongo getMongo2() {
		return m2;
	}	
	*/
	
	
	//根据字段得到头像地址
	public static StringBuffer getAvatarValue(Mongo mg,String userId) {


    	StringBuffer strBuf = new StringBuffer();
		try {
	
	
	        // 获取名为“dbtest”的数据库对象
	        DB db = mg.getDB("users");


	        DBCollection avatars = db.getCollection("avatars");

	        BasicDBObject query=new BasicDBObject();  
	        //query.put("topic_id", 100169);
	        query.put("_id", Long.parseLong(userId));
	        DBCursor cur = avatars.find(query);

	        while (cur.hasNext()) {
	            DBObject object = cur.next();
//System.out.println(object);
	            
//	    		w48: "http://www.mmbang.info/imgs/user_48.gif",
//	    		w160: "http://www.mmbang.info/imgs/user_160.gif",
//	    		w64: "http://www.mmbang.info/imgs/user_64.gif",
//	    		w100: "http://www.mmbang.info/imgs/user_100.gif",
//	    		w24: "http://www.mmbang.info/imgs/user_32.gif",
//	    		w32: "http://www.mmbang.info/imgs/user_32.gif"	            
	            
	            
	            String w24 = StringOperation.getDisplayString(String.valueOf(object.get("w24")));
	            String w32 = StringOperation.getDisplayString(String.valueOf(object.get("w32")));
	            String w48 = StringOperation.getDisplayString(String.valueOf(object.get("w48")));
	            String w64 = StringOperation.getDisplayString(String.valueOf(object.get("w64")));
	            String w100 = StringOperation.getDisplayString(String.valueOf(object.get("w100")));
	            String w160 = StringOperation.getDisplayString(String.valueOf(object.get("w160")));
//System.out.println(fieldName + ":" + fieldValue);
	            strBuf.append("\"w24\":\""+w24+"\"");
	            strBuf.append(",\"w32\":\""+w32+"\"");
	            strBuf.append(",\"w48\":\""+w48+"\"");
	            strBuf.append(",\"w64\":\""+w64+"\"");
	            strBuf.append(",\"w100\":\""+w100+"\"");
	            strBuf.append(",\"w160\":\""+w160+"\"");
	        }
	       
	     
	        cur.close();
	        
	        
        
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}  
        
        return strBuf;
    
	}	
	

	//根据village_id得到village信息
	public static DBObject getVillageObject(Mongo mg,ObjectId id) {


    	StringBuffer strBuf = new StringBuffer();
    	DBObject object = null;
		try {
	
	
	        // 获取名为“dbtest”的数据库对象
	        DB db = mg.getDB("area");


	        DBCollection villages = db.getCollection("village");

	        BasicDBObject query=new BasicDBObject();
	        
	        query.put("_id", id);
	       
	        DBCursor cur = villages.find(query);
	        
	        
//	        DBCursor cur = posts.find({ 'name': { $regex: '*.lnmopy.com', $options: 'i' } } );
	        System.out.println("Record Count:" + cur.count());    
	        
	        
	        while (cur.hasNext()) {
	        	object = cur.next();
	            //System.out.println(object);
	        }
	       
	     
	        cur.close();
	        
	        
        
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}  
        
        return object;
    
	}		
	
	//根据village_id得到village信息
	public static DBObject getUserObject(Mongo mg,ObjectId id) {


    	StringBuffer strBuf = new StringBuffer();
    	DBObject object = null;
		try {
	
	
	        // 获取名为“dbtest”的数据库对象
	        DB db = mg.getDB("area");


	        DBCollection users = db.getCollection("user");

	        BasicDBObject query=new BasicDBObject();
	        
	        query.put("_id", id);
	       
	        DBCursor cur = users.find(query);
	        
	        
//	        DBCursor cur = posts.find({ 'name': { $regex: '*.lnmopy.com', $options: 'i' } } );
	        System.out.println("Record Count:" + cur.count());    
	        
	        
	        while (cur.hasNext()) {
	        	object = cur.next();
	            //System.out.println(object);
	        }
	       
	     
	        cur.close();
	        
	        
        
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}  
        
        return object;
    
	}		

	/**
	 * @param args
	 * @throws UnknownHostException 
	 */
	public static void main(String[] args) throws UnknownHostException {
		// TODO Auto-generated method stub
		
		
		DbTools test = new DbTools();
		Mongo mg = new Mongo(FileRes.getPropValue("dbHost"),27017);
//		System.out.println(test.getAvatarValue(mg, "878"));
		//System.out.println(test.getVillageObject(mg, new ObjectId("52897b58f3817d0b7462a500")));
		
		DBObject object = test.getUserObject(mg, new ObjectId("528b4d331d41c80e39fac00e"));
		if (object!=null) {
			System.out.println(String.valueOf(object.get("name")));
		}
		
		
//		Mongo mg2= new Mongo(FileRes.getPropValue("dbHost2"),Integer.parseInt(FileRes.getPropValue("dbHost2_port")));
		
		
		

	}

}
